package com.atguigu.stack;

/**
 * @author shkstart
 * @create 2021-09-16 13:54
 */
public class StackTest {
    //总的个数
    public int size;
    //头节点
    public Node head;

    public StackTest() {
        size=0;
        head=null;
    }

    //内部类
    class Node{
        //数据
        public Object data;
        //next域
        public Node next;
        //构造器

        public Node(Object data) {
            this.data = data;
        }
    }
    //判断是否为空
    public boolean IsEmpty(){
        return size==0;
    }
    //入栈(头插法)
    public void push(Object value){
        //生成对应的节点
        Node newnode = new Node(value);
        //如果为空栈，那就赋予头节点
        if (size==0){
            head=newnode;
        }else {
            newnode.next=head;
            head=newnode;
        }
        size++;
    }
    //出栈
    public Object pop(){
        if (size>0){
            Node oldnode=head;
            head=head.next;
            size--;
            return oldnode.data;
        }else {
            return new RuntimeException("栈为空，没有数据！");
        }
    }
//    //打印栈
//    public void print(){
//        Node temp=head;
//        while (true){
//            System.out.print(temp.data+"->");
//            if (temp.next==null){
//                break;
//            }
//            temp=temp.next;
//        }
//    }
//打印栈
    public void print(){
        System.out.print("[");
        Node node=head;

        while (size>0){
            if (node==null){
                System.out.println("]");
            }
            if (node.next==null){
                System.out.print(node.data+"]");
            }else {
                System.out.print(node.data+"->");
            }
            node=node.next;
            size--;
        }
    }
    public static void main(String[] args) {
        StackTest stackTest = new StackTest();
        stackTest.push(1);
        stackTest.push(144);
        stackTest.push(155);
        stackTest.push(166);
        stackTest.IsEmpty();
        stackTest.pop();
        stackTest.print();
    }
}
